<template>
	<view>
		<view class="page">
			<u-form :model="shiming" ref="uForm" v-if="shiming.type == 0">
				<u-form-item label-width="144rpx" label="姓名" prop="actualName">
					<u-input v-model="shiming.actualName" placeholder="请输入您的姓名" />
				</u-form-item>
				<u-form-item label-width="144rpx" label="身份证号" prop="licenseNumber">
					<u-input v-model="shiming.licenseNumber" maxlength="18" placeholder="请输入您的身份证号" />
				</u-form-item>
			</u-form>
			<u-form :model="gongpai" ref="uForm" v-if="gongpai.type == 1">
				<u-form-item label-width="144rpx" label="公司" prop="company">
					<u-input v-model="gongpai.company" placeholder="请输入您的公司" />
				</u-form-item>
				<u-form-item label-width="144rpx" label="职位" prop="position">
					<u-input v-model="gongpai.position" placeholder="请输入您的职位" />
				</u-form-item>
			</u-form>
			<u-form :model="mingpian" ref="uForm" v-if="mingpian.type == 2">
				<u-form-item label-width="144rpx" label="姓名" prop="actualName">
					<u-input v-model="mingpian.actualName" placeholder="请输入您的姓名" />
				</u-form-item>
			</u-form>
			<view class="txt">上传图片：</view>
			<view class="upload">
				<u-upload name="path" :header="header" max-count="2" :custom-btn="true" file-list="fileList"
					:action="action" @on-remove="onRemove" @on-Success="onSuccess" width="170" height="170">
					<view slot="addBtn" style="width: 170rpx;height: 170rpx;">
						<image src="https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/my/tianjiaimgsss.png" mode="">
						</image>
					</view>
				</u-upload>
			</view>
		</view>
		<view class="button flex justify-center align-center" @click="submit">提 交</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				action: 'https://shanfu.51fnt.com/attentionList/uploadPicture',
				id: '',
				path: {
					path: 'path'
				},
				header: {
					'Content-Type': 'formData'
				},
				shiming: {
					imageList: [],
					type: Number,
					actualName: '',
					licenseNumber: ''
				},
				gongpai: {
					imageList: [],
					type: Number,
					company: '',
					position: ''
				},
				mingpian: {
					imageList: [],
					type: Number,
					actualName: ''
				},
				fileList: [],
				postList: {
					imageList: [],
					type: '',
					actualName: '',
					licenseNumber: '',
					company: '',
					position: '',
				},
				rules: {
					actualName: [
						// 对name字段进行长度验证
						{
							required: true,
							message: '请填写正确姓名',
							trigger: 'blur'
						},
					],
					licenseNumber: [
						// 对name字段进行长度验证
						{
							required: true,
							min: 18,
							message: '请填写正确的身份证号',
							trigger: 'blur'
						},
					],
					company: [
						// 对name字段进行长度验证
						{
							required: true,
							message: '请填写您的公司',
							trigger: 'blur'
						},
					],
					position: [
						// 对name字段进行长度验证
						{
							required: true,
							message: '请填写您的职位',
							trigger: 'blur'
						},
					]
				},
			}
		},
		onLoad(e) {
			this.mingpian.type = e.id
			this.shiming.type = e.id
			this.gongpai.type = e.id
			if (e.id == 0) {
				uni.setNavigationBarTitle({
					title: '实名认证'
				})
			} else if (e.id == 1) {
				uni.setNavigationBarTitle({
					title: '工牌认证'
				})
			} else if (e.id == 2) {
				uni.setNavigationBarTitle({
					title: '名片认证'
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			// this.$refs.uForm.setRules(this.rules1);
			// this.$refs.uForm.setRules(this.rules2);
		},
		methods: {
			onSuccess(res, index, lists) {
				//页面上定义的临时存放图片的对象，提示也保存后台返回的图片名称
				if (res) {
					// let currentFile = {};
					// currentFile.image = res.data.picture[0];
					// currentFile.type = 0;
					// //成功上传一个图片就往fileList里面添加一个图片对象
					console.log(res.data.picture[0].url)
					this.shiming.imageList.push(res.data.picture[0].url);
					this.gongpai.imageList.push(res.data.picture[0].url);
					this.mingpian.imageList.push(res.data.picture[0].url);
					//this.form.pictureUrl为后台图片字段来保存字符串类型的图片集合
				}
			},
			//删除一张图片的回调，lists这是成功删除一个图片后，还剩余的图片集合
			onRemove(data, lists) {
				this.fileList.splice(data, 1)
				this.shiming.imageList.splice(data, 1)
				this.gongpai.imageList.splice(data, 1)
				this.mingpian.imageList.splice(data, 1)

			},
			submit() {
				switch (this.mingpian.type) {
					case '0':
						if (this.shiming.imageList.length) {
							this.$refs.uForm.validate(valid => {
								if (valid) {
									this.$post('/user/userAuthentication', this.shiming)
									uni.navigateBack({
										delta: 1
									})
								} else {
									console.log('验证失败');
								}
							})
						} else {
							uni.showToast({
								title: '请添加图片',
								icon: 'none'
							})
						};
						break;
					case '1':
						if (this.mingpian.imageList.length) {
							this.$refs.uForm.validate(valid => {
								if (valid) {
									this.$post('/user/userAuthentication', this.gongpai)
									uni.navigateBack({
										delta: 1
									})
								} else {
									console.log('验证失败');
								}
							})
						} else {
							uni.showToast({
								title: '请添加图片',
								icon: 'none'
							})
						};
						break;
					case '2':
						if (this.mingpian.imageList.length) {
							this.$refs.uForm.validate(valid => {
								if (valid) {
									this.$post('/user/userAuthentication', this.mingpian)
									uni.navigateBack({
										delta: 1
									})
								} else {
									console.log('验证失败');
								}
							})
						} else {
							uni.showToast({
								title: '请添加图片',
								icon: 'none'
							})
						};
						break;
				}
			}
		}
	}
</script>

<style lang="scss">
	.page {
		padding: 30rpx;

		.txt {
			padding-top: 40rpx;
			font-size: 34rpx;
			margin-bottom: 40rpx;
		}

	}

	.button{
		position: fixed;
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		width: 690rpx;
		left:30rpx;
		height: 75rpx;
		background: #0265EB;
		border-radius: 38rpx;
		line-height: 75rpx;
		font-weight: 500;
		color: #FFFFFF;
		font-size: 32rpx;
		box-shadow: 0rpx 10rpx 10rpx rgba(12,108,240, .4);
	}
</style>
